# -*- coding: utf-8 -*-

#
# fusion.http.Profile
#
# Copyright 2008 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Harri Hämäläinen <hhamalai@iki.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from fusion.http.FusionApp import invitations, FusionApp, Response
from string import Template

class Domain(FusionApp):

    def domain_datasets(self):
        datasets = self.db.list_content(self.iid)
        output = ''.join(['''<a href="http://%s.hexlabel.net/presentation/web/index.html">%s</a><br />''' 
                        % (iid, self.db.get(iid, 'info/title.txt') or iid) for iid in datasets])
        if output:
            output = '<h3>Content</h3>' + output
        return output
    

    def accept_invitation(self, iid):
        if iid and invitations.has_key(iid):
            result = invitations[iid].accept_new(self.db)
            if result == 'OK':
                invitations.pop(iid)
            else:
                self.add_notification(result, target='ack_invitation')
        else:
            self.add_notification('Invitation does not exist.',
                                  target='ack_invitation')        
    
    
    def decline_invitation(self, iid):
        if iid and invitations.has_key(iid):
            invitations.pop(iid)
        else:
            self.add_notification('Invitation does not exist.',
                                  target='ack_invitation')
            
            
    def pending_invitation(self, env):
        global invitations
        if self.iid in invitations.keys():
            info = env['fusion.db'].fetch(env['fusion.iid'], '', type='json')
            msg = 'You have a pending friendship invitation from the owner of this profile.'
            if info['type'] == 'group':
                msg = 'You have a pending membership invitation into this group.'
            return Template('''
        <tr>
        <td colspan="2">
        <div id="accept_invitation_field" class="inputframe safetycolor">
        ${message}
        <br />
        <form action="" method="POST">
        <input type="hidden" name="iid" value="${iid}" />
        <input type="submit" name="accept_invitation" value="Accept" /> 
        <input type="submit" name="decline_invitation" value="Decline" />
        </form>
        </div>
        <div id="fetch_invitation_result">
        ${notifs}
        </div>
        </td>
        </tr>
        ''').substitute(iid = self.iid,
                        message = msg,
                        notifs = '<br />'.join(self.get_notifications('ack_invitation')))
        else:
            return ''
        
        
    def output_profile(self):
        response = Response()
        response.add_header('Content-Type', 'text/html;charset=utf-8')
        if self.querydict.get('edit'):
            response.append_output(self.output_edit_profile())  
        else:
            response.append_output(self.output_view_profile())
        return response
    
